layui.use(['form', 'layedit', 'upload','laydate'], function () {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;


    laydate.render({
        elem: '#createTime'
    });

    laydate.render({
        elem: '#updateTime',
        type:'datetime'
    });

    form.verify({
        advertisementId: function (value) {
            if (value.length > 64) {
                return '长度不能大于64';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        creatorId: function (value) {
            if (value.length > 64) {
                return '长度不能大于64';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        advertisementImg: function (value) {
            if (value.length > 256) {
                return '长度不能大于256';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        advertisementUrl: function (value) {
            if (value.length > 256) {
                return '长度不能大于256';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        clicksCount: function (value) {
            if (value.length > 22) {
                return '长度不能大于22';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        createTime: function (value) {
            if (value.length > 7) {
                return '长度不能大于7';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        updateTime: function (value) {
            if (value.length > 7) {
                return '长度不能大于7';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },

    });
    var $ = layui.jquery
        ,upload = layui.upload;
    //普通图片上传
    var uploadInst = upload.render({
        elem: '#accPhoto'
        , url: '/uploadController/upload'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#accPhotoimg').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code == 0) {
                return layer.msg('上传失败');
            } else {
                $("#advertisementImg").val(res.filename);
                return layer.msg('上传成功');
            }
            //上传成功
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#accPhoto');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
    //监听提交
    form.on('submit(formDemo)', function (data) {
        $ = layui.jquery;
        layer = layui.layer;
        var waitIndex = '';

        $.ajax({
            url: "/cmsservice/newsAdvertisementController/update",
            type: "POST",
            data: data.field,
            dataType: 'json',
            beforeSend: function () {
                waitIndex = layer.load();//删除前打开遮挡层
            },
            success: function (returnData) {
                if (returnData.code == 500) {
                    layer.msg(returnData.message, {icon: 2, time: 1500, shade: 0.1}, function () {
                    });

                } else {
                    layer.msg(returnData.message, {icon: 1, time: 1500, shade: 0.1}, function () {
                        top.winui.window.close('edit');
                    });

                }


            }, complete: function () {
                layer.close(waitIndex);
            }
        });

        return false;
    });
});